/*
 *	Copyright (C) 2008 - Mauricio Bieze Stefani
 *	This file is part of the Insano Game Engine.
 *
 *	Insano is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *
 *	Insano is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with Insano.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "ServerConfig.h"
#include "Account.h"
#include <string>
#include <string.h>
#include <iostream>
#include "SqliteCB.h"
#include "Protocolo.h"
#include <stdlib.h>

using namespace std;

int DB_read_config(void *config, int argc, char **argv, char **col)
{
	// SQLite sample: http://www.sqlite.org/quickstart.html
	for(int i=0; i<argc; i++)
		if(strcmp(col[i],"TCP_PORT")==0)
			((ServerConfig*)config)->port=atoi(argv[i]);
	return 1; // abort
}

int DB_read_account(void *account, int argc, char **argv, char **col)
{
	for(int i=0; i<argc; i++)
		if(strcmp(col[i],"ACCOUNT_ID")==0)
			((Account*)account)->account_id=atoi(argv[i]);
		else if(strcmp(col[i],"USER_NAME")==0)
			((Account*)account)->user_name=std::string(argv[i]);
	return 1; // abort
}

int DB_read_char_list(void *charList, int argc, char **argv, char **col)
{
	ProtoCharList *dbcl=(ProtoCharList*) charList;
	unsigned char current=dbcl->numChars;

	for(int i=0; i<argc; i++)
		if(strcmp(col[i],"CHAR_ID")==0)
			dbcl->chars[current].id=atoi(argv[i]);
		else if(strcmp(col[i],"CHAR_NAME")==0)
			strncpy(dbcl->chars[current].name, argv[i], MAX_NAME);
		else if(strcmp(col[i],"RACE")==0)
			dbcl->chars[current].race=atoi(argv[i]);
		else if(strcmp(col[i],"GENDER")==0)
			dbcl->chars[current].gender=atoi(argv[i]);

	dbcl->numChars++;

	return 0;
}

int DB_read_char(void *character, int argc, char **argv, char **col)
{
	ProtoCharDef *cd=(ProtoCharDef*)character;

	for(int i=0; i<argc; i++)
		if(strcmp(col[i],"CHAR_ID")==0)
			cd->id=atoi(argv[i]);
		else if(strcmp(col[i],"CHAR_NAME")==0)
			strncpy(cd->name, argv[i], MAX_NAME);
		else if(strcmp(col[i],"RACE")==0)
			cd->race=atoi(argv[i]);
		else if(strcmp(col[i],"GENDER")==0)
			cd->gender=atoi(argv[i]);

	return 1;
}
